/*
Java board game library.
Copyright (C) 2011  Deepesh Garg

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/


package d.g.boardgames.chess ;


import java.awt.Color;
import java.awt.Image ;
import java.io.* ;
import java.util.List;

import javax.imageio.ImageIO ;

import d.g.boardgames.Board;
import d.g.boardgames.Cell;
import d.g.boardgames.DefaultBoardRenderProperties;
import d.g.boardgames.chess.ChessCellState.PieceColor;
import d.g.boardgames.chess.ChessCellState.PieceType;

import static d.g.boardgames.soccer.SoccerBoard.*;

public class ChessBoardRenderProperties extends DefaultBoardRenderProperties<ChessBoard> {

	Cell selectedCell;
	List<Cell> validMoves;
	
	Image backgroundImage ;
	Image blackPawn ;
	Image whitePawn ;
	Image blackRook ;
	Image whiteRook ;	
	Image blackKnight ;
	Image whiteKnight ;	
	Image blackBishop ;
	Image whiteBishop ;	
	Image blackQueen ;
	Image whiteQueen ;	
	Image blackKing ;
	Image whiteKing ;
	Image bottomBorder ;
	Image topBorder ;
	Image leftBorder ;
	Image rightBorder ;
	
	boolean displayHint;
	//Image blueImage ;
	//Image redBallImage ;
	//Image blueBallImage ;

	public ChessBoardRenderProperties () {
		this("default");
	}
	
	public ChessBoardRenderProperties (String theme) {
		try {
			blackPawn = ImageIO.read (getClass().getResource("/Data/chess/images/" + theme + "/pawn-b.png")) ;
			whitePawn = ImageIO.read (getClass().getResource("/Data/chess/images/" + theme + "/pawn-w.png")) ;
			blackRook = ImageIO.read (getClass().getResource("/Data/chess/images/" + theme + "/rook-b.png")) ;
			whiteRook = ImageIO.read (getClass().getResource("/Data/chess/images/" + theme + "/rook-w.png")) ;
			blackKnight = ImageIO.read (getClass().getResource("/Data/chess/images/" + theme + "/knight-b.png")) ;
			whiteKnight = ImageIO.read (getClass().getResource("/Data/chess/images/" + theme + "/knight-w.png")) ;
			blackBishop = ImageIO.read (getClass().getResource("/Data/chess/images/" + theme + "/bishop-b.png")) ;
			whiteBishop = ImageIO.read (getClass().getResource("/Data/chess/images/" + theme + "/bishop-w.png")) ;
			blackQueen = ImageIO.read (getClass().getResource("/Data/chess/images/" + theme + "/queen-b.png")) ;
			whiteQueen = ImageIO.read (getClass().getResource("/Data/chess/images/" + theme + "/queen-w.png")) ;
			blackKing = ImageIO.read (getClass().getResource("/Data/chess/images/" + theme + "/king-b.png")) ;
			whiteKing = ImageIO.read (getClass().getResource("/Data/chess/images/" + theme + "/king-w.png")) ;
			bottomBorder = ImageIO.read (getClass().getResource("/Data/chess/images/" + theme + "/bottom-bar.png")) ;
			topBorder = ImageIO.read (getClass().getResource("/Data/chess/images/" + theme + "/top-bar.png")) ;
			leftBorder = ImageIO.read (getClass().getResource("/Data/chess/images/" + theme + "/left-bar.png")) ;
			rightBorder = ImageIO.read (getClass().getResource("/Data/chess/images/" + theme + "/right-bar.png")) ;
		} catch (Exception e) {
			e.printStackTrace() ;
		}
	}

	public Image getBackgroundImage () {
		return backgroundImage ;
	}

	Color lightCellColor = new Color(0xff, 0xCC, 0x99);
	Color darkCellColor = new Color(0xcc, 0x66, 0x33);
	@Override
	public Color getCellColor (ChessBoard board, int row, int col) {
		if ((row + col) % 2 == 0 ) {
			//return Color.LIGHT_GRAY;
			return lightCellColor;
		} else {
			//return Color.GRAY;
			return darkCellColor;
		}
	}
	
	public Image getCellImage (ChessBoard board, int row, int col) {
		ChessCellState cellState = board.getCellState(row, col);
		if (cellState == null)
			return null ;
		
		if (cellState.pieceColor == PieceColor.BLACK) {
			switch (cellState.pieceType) {
				case PAWN: return blackPawn;
				case ROOK: return blackRook;
				case KNIGHT: return blackKnight;
				case BISHOP: return blackBishop;
				case QUEEN: return blackQueen;
				case KING: return blackKing;
			}
		}
		if (cellState.pieceColor == PieceColor.WHITE) {
			switch (cellState.pieceType) {
				case PAWN: return whitePawn;
				case ROOK: return whiteRook;
				case KNIGHT: return whiteKnight;
				case BISHOP: return whiteBishop;
				case QUEEN: return whiteQueen;
				case KING: return whiteKing;
			}
		}		
		return null;
	}
	
	public Image getBottomBorder (boolean flip) {
		if (!flip) {
			return bottomBorder ;
		} else {
			return null;
		}
	}
	
	public Image getTopBorder (boolean flip) {
		if (flip) {
			return topBorder ;
		} else {
			return null;
		}
	}
	
	public Image getLeftBorder (boolean flip) {
		if (!flip) {
			return leftBorder ;
		} else {
			return null;
		}
	}
	
	public Image getRightBorder (boolean flip) {
		if (flip) {
			return rightBorder ;
		} else {
			return null;
		}
	}
	
	public Color getCellBorderColor (ChessBoard board, int row, int col) {
		Cell cell = new Cell(row, col);
		if (selectedCell != null && selectedCell.equals(cell)) {
			return Color.RED;
		}
		if (validMoves != null && validMoves.contains(cell)) {
			return Color.GREEN;
		}
		return null;
	}
	
	public boolean isGridOn() {
		return true;
	}

	public Cell getSelectedCell() {
		return selectedCell;
	}

	public void setSelectedCell(Cell selectedCell) {
		this.selectedCell = selectedCell;
	}

	public List<Cell> getValidMoves() {
		return validMoves;
	}

	public void setValidMoves(List<Cell> validMoves) {
		this.validMoves = validMoves;
	}
	
	public float getPaddingX() {
		return 10;
	}

	public float getPaddingY() {
		return 10;
	}

	public float getOffsetY() {
		return 0;
	}

	public boolean isDisplayHint() {
		return displayHint;
	}

	public void setDisplayHint(boolean displayHint) {
		this.displayHint = displayHint;
	}
	
}
